package cn.ht.jywrwater.module.ht.service.waterdevices;

import cn.ht.jywrwater.module.ht.controller.admin.watersurvey.vo.WaterSurveyCountVO;
import cn.ht.jywrwater.module.ht.controller.admin.watersurvey.vo.WaterSurveyTypeListVO;
import cn.ht.jywrwater.module.ht.dal.dataobject.watersurvey.WaterSurveyDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.ht.jywrwater.module.ht.controller.admin.waterdevices.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterdevices.WaterDevicesDO;
import cn.ht.jywrwater.framework.common.pojo.PageResult;

import cn.ht.jywrwater.module.ht.convert.waterdevices.WaterDevicesConvert;
import cn.ht.jywrwater.module.ht.dal.mysql.waterdevices.WaterDevicesMapper;

import static cn.ht.jywrwater.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ht.jywrwater.module.ht.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 设备 Service 实现类
 *
 * @author hzx
 */
@Service
@Validated
public class WaterDevicesServiceImpl implements WaterDevicesService {

    @Resource
    private WaterDevicesMapper waterDevicesMapper;

    @Override
    public Long createWaterDevices(WaterDevicesCreateReqVO createReqVO) {
        // 插入
        WaterDevicesDO waterDevices = WaterDevicesConvert.INSTANCE.convert(createReqVO);
        waterDevicesMapper.insert(waterDevices);
        // 返回
        return waterDevices.getId();
    }

    @Override
    public void updateWaterDevices(WaterDevicesUpdateReqVO updateReqVO) {
        // 校验存在
        validateWaterDevicesExists(updateReqVO.getId());
        // 更新
        WaterDevicesDO updateObj = WaterDevicesConvert.INSTANCE.convert(updateReqVO);
        waterDevicesMapper.updateById(updateObj);
    }

    @Override
    public void deleteWaterDevices(Long id) {
        // 校验存在
        validateWaterDevicesExists(id);
        // 删除
        waterDevicesMapper.deleteById(id);
    }

    private void validateWaterDevicesExists(Long id) {
        if (waterDevicesMapper.selectById(id) == null) {
            throw exception(WATER_DEVICES_NOT_EXISTS);
        }
    }

    @Override
    public WaterDevicesDO getWaterDevices(Long id) {
        return waterDevicesMapper.selectById(id);
    }

    @Override
    public List<WaterDevicesDO> getWaterDevicesList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return waterDevicesMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WaterDevicesDO> getWaterDevicesPage(WaterDevicesPageReqVO pageReqVO) {
        return waterDevicesMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WaterDevicesDO> getWaterDevicesList(WaterDevicesExportReqVO exportReqVO) {
        return waterDevicesMapper.selectList(exportReqVO);
    }

    @Override
    public List<WaterDeviceTypeListVO> getWaterDeviceTypesList(Collection<Integer> types) {
        List<WaterDeviceTypeListVO> responseVOList = new ArrayList<>();
        for (Integer type:types){
            WaterDeviceTypeListVO waterDeviceTypeVO =new WaterDeviceTypeListVO();
            List<WaterDevicesDO> waterDeviceTypeDOList = waterDevicesMapper.selectList("device_type", type);
            waterDeviceTypeVO.setType(type);
            waterDeviceTypeVO.setWaterDeviceList(waterDeviceTypeDOList);
            Integer online = (int)waterDeviceTypeDOList.stream().filter(device -> device.getUseStatus()==1).count();
            waterDeviceTypeVO.setOnline(online);
            waterDeviceTypeVO.setCount(waterDeviceTypeDOList.size());
            responseVOList.add(waterDeviceTypeVO);
        }
        return responseVOList;
    }

    @Override
    public WaterDeviceCountVO countByTypeAndStatus(Integer type, Integer status) {
        List<WaterDevicesDO> waterTypeDeviceStatusList = new ArrayList<>();
        List<WaterDevicesDO> waterTypeDeviceList = waterDevicesMapper.selectList("device_type", type);
        if (status==1){
            for (WaterDevicesDO waterDevice:waterTypeDeviceList){
                if (waterDevice.getUseStatus() == 1){
                    waterTypeDeviceStatusList.add(waterDevice);
                }
            }
        }
        if (status==0){
            for (WaterDevicesDO waterDevice:waterTypeDeviceList){
                if (waterDevice.getUseStatus() == 0){
                    waterTypeDeviceStatusList.add(waterDevice);
                }
            }
        }
        WaterDeviceCountVO result = new WaterDeviceCountVO();
        result.setStatus(status);
        result.setType(type);
        result.setNums(waterTypeDeviceStatusList.size());
        result.setWaterDevicesDOList(waterTypeDeviceStatusList);

        return result;
    }

}
